package com.movie.repository;

import com.movie.entity.Review;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {
    
    @Query("SELECT r FROM Review r WHERE r.comment.id = :commentId")
    Optional<Review> findByCommentId(@Param("commentId") Long commentId);
    
    @Query("SELECT r FROM Review r WHERE r.comment.movie.id = :movieId AND r.status = :status")
    List<Review> findByMovieIdAndStatus(@Param("movieId") Long movieId, @Param("status") String status);
    
    @Query("SELECT r FROM Review r WHERE r.comment.movie.id = :movieId")
    List<Review> findByMovieId(@Param("movieId") Long movieId);

    @Query("SELECT r FROM Review r WHERE r.comment.id IN :commentIds")
    List<Review> findByCommentIds(@Param("commentIds") List<Long> commentIds);
    
    @Query("SELECT r FROM Review r WHERE r.status = :status")
    Page<Review> findByStatus(@Param("status") String status, Pageable pageable);
    
    @Query("SELECT r FROM Review r WHERE r.status = :status AND r.comment.movie.id IS NOT NULL")
    Page<Review> findPendingReviewsByType(@Param("status") String status, Pageable pageable);
}